package com.samsung.spen.sshare.networking.networkservice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.apache.http.conn.util.InetAddressUtils;

import android.annotation.TargetApi;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Build;
import android.util.Log;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class Utils {
	private static String TAG = "RETURN_DATA";
	public static final String WIFI_DIRECT_INTERFACE = "p2p-wlan0-0";
	public static final String WLAN0 = "wlan0";

	public static boolean checkNetworkState(Context context) {

		WifiManager wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		if (wifiManager.isWifiEnabled())
			Log.d(TAG, "wifi is on 1");

//		WifiInfo info = wifiManager.getConnectionInfo();
//
//		if (WifiInfo.getDetailedStateOf(info.getSupplicantState()) == NetworkInfo.DetailedState.CONNECTED) {
//			Log.d(TAG, "wifi is on 2");
//		}

		// ConnectivityManager connectivityManager = (ConnectivityManager)
		// context.getSystemService(Context.CONNECTIVITY_SERVICE);
		// NetworkInfo networkInfo = connectivityManager.getAllNetworkInfo()[0];
		// if(networkInfo==null) return false;
		// Log.d("RETURN_DATA","TYPE = " + networkInfo.getType() +"--"+
		// networkInfo.getTypeName() );
		// Log.d("RETURN_DATA","SUBTYPE = "+networkInfo.getSubtype()+"--"+
		// networkInfo.getSubtypeName());
		return false;
	}

	/**
	 * get IP address of devices at _interface
	 * 
	 * @param _interface
	 * @return IP address or 0.0.0.0 (get error when getting address)
	 */
	public static String getIPAddress(String _interface) {
		return getIPAddress(true, _interface);
	}

	/**
	 * get IP address of this device
	 * 
	 * @param useIPv4
	 * @param _interface
	 * @return IP address or 0.0.0.0 if can't get IP address
	 */
	private static String getIPAddress(boolean useIPv4, String _interface) {
		try {
			List<NetworkInterface> interfaces = Collections
					.list(NetworkInterface.getNetworkInterfaces());
			for (NetworkInterface intf : interfaces) {
				// Log.d("RETURN_DATA", intf.getDisplayName());
				if (!intf.getDisplayName().equals(_interface))
					continue;
				List<InetAddress> addrs = Collections.list(intf
						.getInetAddresses());
				for (InetAddress addr : addrs) {
					if (!addr.isLoopbackAddress()) {
						String sAddr = addr.getHostAddress().toUpperCase();
						boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
						if (useIPv4) {
							if (isIPv4)
								return sAddr;
						} else {
							if (!isIPv4) {
								int delim = sAddr.indexOf('%'); // drop ip6 port
																// suffix
								return delim < 0 ? sAddr : sAddr.substring(0,
										delim);
							}
						}
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} // for now eat exceptions
		return "0.0.0.0";
	}

	// public static String getIPAddress(String _interface) {
	// HashMap< String, String> addr = new HashMap<String, String>();
	// Process process = null;
	// try {
	// process = Runtime.getRuntime().exec("netcfg",null,null);
	// BufferedReader buffer = new BufferedReader(new InputStreamReader(
	// process.getInputStream()));
	// String line;
	// while((line = buffer.readLine())!=null){
	// Log.d("RETURN_DATA", "Line = " +line);
	// String[] subLine = line.split("\t");
	// for (String string : subLine) {
	// Log.d("RETURN_DATA", string);
	// }
	//
	// }
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// return "";
	// }

}
